import 'package:flutter/foundation.dart' show kIsWeb;

class Mood {
  final int id;
  final int moodType; // 1-5，表示情绪等级，1最差，5最好
  final int intensity;
  final List<String> emotions;
  final List<String> triggers;
  final List<String> bodyReactions;
  final String description;
  final String? imagePath; // 在 Web 平台上存储 base64 编码的图片数据
  final DateTime createdAt;
  final String date; // 添加日期字段

  Mood({
    required this.id,
    required this.moodType,
    required this.intensity,
    required this.emotions,
    required this.triggers,
    required this.bodyReactions,
    required this.description,
    this.imagePath,
    required this.createdAt,
    required this.date,
  });

  // 从数据库映射创建Mood对象
  factory Mood.fromMap(Map<String, dynamic> map) {
    return Mood(
      id: map['id'] as int,
      moodType: map['moodType'] as int,
      intensity: map['intensity'] as int,
      emotions: map['emotions'].toString().split(',').where((e) => e.isNotEmpty).toList(),
      triggers: map['triggers'].toString().split(',').where((t) => t.isNotEmpty).toList(),
      bodyReactions: map['bodyReactions'].toString().split(',').where((r) => r.isNotEmpty).toList(),
      description: map['description'] as String,
      imagePath: map['imagePath'] as String?,
      createdAt: DateTime.parse(map['createdAt'] as String),
      date: map['date'] as String,
    );
  }

  // 将Mood对象转换为数据库映射
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'moodType': moodType,
      'intensity': intensity,
      'emotions': emotions.join(','),
      'triggers': triggers.join(','),
      'bodyReactions': bodyReactions.join(','),
      'description': description,
      'imagePath': imagePath,
      'createdAt': createdAt.toIso8601String(),
      'date': date,
    };
  }

  // 获取情绪类型的文字描述
  String get moodTypeName {
    String baseName = switch (moodType) {
      1 => '很差',
      2 => '较差',
      3 => '一般',
      4 => '较好',
      5 => '很好',
      _ => '未知',
    };
    
    // 如果有情绪词，添加到基础名称后
    if (emotions.isNotEmpty) {
      return '$baseName，${emotions.join("，")}';
    }
    
    return baseName;
  }

  // 复制并修改Mood对象
  Mood copyWith({
    int? id,
    int? moodType,
    int? intensity,
    List<String>? emotions,
    List<String>? triggers,
    List<String>? bodyReactions,
    String? description,
    String? imagePath,
    DateTime? createdAt,
    String? date,
  }) {
    return Mood(
      id: id ?? this.id,
      moodType: moodType ?? this.moodType,
      intensity: intensity ?? this.intensity,
      emotions: emotions ?? List.from(this.emotions),
      triggers: triggers ?? List.from(this.triggers),
      bodyReactions: bodyReactions ?? List.from(this.bodyReactions),
      description: description ?? this.description,
      imagePath: imagePath ?? this.imagePath,
      createdAt: createdAt ?? this.createdAt,
      date: date ?? this.date,
    );
  }
} 